{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# SAKSHAT提供的按键使用方法\n",
    "当前SAKSHAT为我们提供的方法参考：http://wiki.nxez.com/saks:sdk:libraries:tactrow\n",
    "\n",
    "这里就直接摘录了，以下是创百科的内容:\n",
    "\n",
    "TactRow\n",
    "\n",
    "轻触开关组类，配置一组轻触开关的触发事件并随时读取开关即时状态。\n",
    "方法\n",
    "\n",
    "__init__(self, pin, real_true = GPIO.HIGH)\n",
    "\n",
    "初始化对象，设置引脚和触发电平（高电平或低电平触发）。pins 为 IO 引脚数组。\n",
    "\n",
    "is_on(self, index)\n",
    "\n",
    "返回当前轻触开关的按下状态。true 或 false。index 为 LED 编号，从 0 开始。\n",
    "\n",
    "row_status(self)\n",
    "\n",
    "返回当前轻触开关组的工作状态的数组。\n",
    "\n",
    "items(self)\n",
    "\n",
    "返回当前轻触开关组的对象数组。 \n",
    "\n",
    "对于按键可以安装按键触发时的回调函数，回调函数复制给tact_event_handler方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#输出按键状态\n",
    "import time\n",
    "from sakshat import SAKSHAT\n",
    "from sakspins import SAKSPins as PINS\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #Declare the SAKS Board\n",
    "        SAKS = SAKSHAT()\n",
    "        \n",
    "        while True:\n",
    "            #获取按键状态\n",
    "            blue_button = SAKS.tactrow.is_on(0)\n",
    "            yello_button = SAKS.tactrow.is_on(1)\n",
    "            \n",
    "            if blue_button == False:\n",
    "                print(\"蓝色未按下\")\n",
    "            else:\n",
    "                print(\"蓝色已按下\")\n",
    "                \n",
    "            if yello_button == False:\n",
    "                print(\"黄色未按下\")\n",
    "            else:\n",
    "                print(\"黄色已按下\")\n",
    "\n",
    "            time.sleep(1)\n",
    "            \n",
    "    except KeyboardInterrupt:\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#输出按键组状态\n",
    "import time\n",
    "from sakshat import SAKSHAT\n",
    "from sakspins import SAKSPins as PINS\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #Declare the SAKS Board\n",
    "        SAKS = SAKSHAT()\n",
    "        \n",
    "        while True:\n",
    "            #获取按键组状态\n",
    "            key_status = SAKS.tactrow.row_status\n",
    "            \n",
    "            #打印安装状态\n",
    "            print(\"按键组状态为\" + str(key_status))\n",
    "\n",
    "            time.sleep(1)\n",
    "            \n",
    "    except KeyboardInterrupt:\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#按键事件触发方式操作\n",
    "import time\n",
    "from sakshat import SAKSHAT\n",
    "from sakspins import SAKSPins as PINS\n",
    "\n",
    "#在检测到轻触开关触发时自动执行此函数\n",
    "def key_event_handler(pin, status):\n",
    "    '''\n",
    "    called while the status of tacts changed\n",
    "    :param pin: pin number which stauts of tact is changed\n",
    "    :param status: current status\n",
    "    :return: void\n",
    "    '''\n",
    "    \n",
    "    #判断是否是右边的轻触开关被触发，并且是在被按下\n",
    "    if pin == PINS.TACT_RIGHT and status == True:   \n",
    "        print(\"黄色按键被按下\")\n",
    "\n",
    "    #判断是否是右边的轻触开关被触发，并且是在被按下\n",
    "    if pin == PINS.TACT_RIGHT and status == False:   \n",
    "        print(\"黄色按键被释放\")\n",
    "        \n",
    "    #判断是否是右边的轻触开关被触发，并且是在被按下\n",
    "    if pin == PINS.TACT_LEFT and status == True:   \n",
    "        print(\"蓝色按键被按下\")\n",
    "\n",
    "    #判断是否是右边的轻触开关被触发，并且是在被按下\n",
    "    if pin == PINS.TACT_LEFT and status == False:   \n",
    "        print(\"蓝色按键被释放\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #Declare the SAKS Board\n",
    "        SAKS = SAKSHAT()\n",
    "        \n",
    "        #设定轻触开关回调函数\n",
    "        SAKS.tact_event_handler = key_event_handler\n",
    "            \n",
    "    except KeyboardInterrupt:\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
